package com.cqc.googleplay.activity;

import com.cqc.googleplay.R;
import com.cqc.googleplay.fragment.AboutFragment;
import com.cqc.googleplay.fragment.ExitFragment;
import com.cqc.googleplay.fragment.FeedBackFragment;
import com.cqc.googleplay.fragment.HomeFragment;
import com.cqc.googleplay.fragment.ScanFragment;
import com.cqc.googleplay.fragment.SettingFragment;
import com.cqc.googleplay.fragment.ThemeFragment;
import com.cqc.googleplay.fragment.UpdateFragment;

import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements OnClickListener {

	private DrawerLayout drawerLayout;
	private ActionBarDrawerToggle toggle;
	private RelativeLayout home_layout;
	private RelativeLayout setting_layout;
	private RelativeLayout theme_layout;
	private RelativeLayout scans_layout;
	private RelativeLayout feedback_layout;
	private RelativeLayout updates_layout;
	private RelativeLayout about_layout;
	private RelativeLayout exit_layout;
	private FrameLayout frame_home;
	private FragmentManager fm;
	private HomeFragment homeFrag;
	private SettingFragment setFrag;
	private ThemeFragment themeFrag;
	private ScanFragment scanFrag;
	private UpdateFragment updateFrag;
	private AboutFragment aboutFrag;
	private ExitFragment exitFragment;

	private FragmentTransaction transaction;
	private TextView tv_home;
	private TextView tv_setting;
	private TextView tv_theme;
	private TextView tv_scans;
	private TextView tv_feedback;
	private TextView tv_updates;
	private TextView tv_about;
	private TextView tv_exit;
	private FeedBackFragment feedBackFrag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
	}

	private void initViews() {
		initActionBar();
		initDrawerLayout();
		findMenuViews();
		initMenuView();
		home_layout.performClick();
	}

	private void initMenuView() {
		fm = getSupportFragmentManager();
		home_layout.setOnClickListener(this);
		setting_layout.setOnClickListener(this);
		theme_layout.setOnClickListener(this);
		scans_layout.setOnClickListener(this);
		feedback_layout.setOnClickListener(this);
		updates_layout.setOnClickListener(this);
		about_layout.setOnClickListener(this);
		exit_layout.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		setAllFalse();
		transaction = fm.beginTransaction();
		hideAllFragment();//先创建FragmentTransaction对象，在隐藏
		switch (v.getId()) {
		case R.id.home_layout:
			tv_home.setSelected(true);
			if (homeFrag == null) {
				homeFrag = new HomeFragment();
				transaction.add(R.id.frame_home, homeFrag, "HomeFragment");
			}
			transaction.show(homeFrag);
			break;
		case R.id.setting_layout:
			tv_setting.setSelected(true);
			if (setFrag == null) {
				setFrag = new SettingFragment();
				transaction.add(R.id.frame_home, setFrag, "SettingFragment");
			}
			transaction.show(setFrag);
			break;
		case R.id.theme_layout:
			tv_theme.setSelected(true);
			if (themeFrag == null) {
				themeFrag = new ThemeFragment();
				transaction.add(R.id.frame_home, themeFrag, "ThemeFragment");
			}
			transaction.show(themeFrag);
			break;
		case R.id.scans_layout:
			tv_scans.setSelected(true);
			if (scanFrag == null) {
				scanFrag = new ScanFragment();
				transaction.add(R.id.frame_home, scanFrag, "ScanFragment");
			}
			transaction.show(scanFrag);
			break;
		case R.id.feedback_layout:
			tv_feedback.setSelected(true);
			if (feedBackFrag == null) {
				feedBackFrag = new FeedBackFragment();
				transaction.add(R.id.frame_home, feedBackFrag, "FeedBackFragment");
			}
			transaction.show(feedBackFrag);
			break;
		case R.id.updates_layout:
			tv_updates.setSelected(true);
			if (updateFrag == null) {
				updateFrag = new UpdateFragment();
				transaction.add(R.id.frame_home, updateFrag, "UpdateFragment");
			}
			transaction.show(updateFrag);
			break;
		case R.id.about_layout:
			tv_about.setSelected(true);
			if (aboutFrag == null) {
				aboutFrag = new AboutFragment();
				transaction.add(R.id.frame_home, aboutFrag, "AboutFragment");
			}
			transaction.show(aboutFrag);
			break;
		case R.id.exit_layout:
			tv_exit.setSelected(true);
			if (exitFragment == null) {
				exitFragment = new ExitFragment();
				transaction.add(R.id.frame_home, exitFragment, "ExitFragment");
			}
			transaction.show(exitFragment);
			break;
		}
		drawerLayout.closeDrawers();
		transaction.commit();
	}

	private void setAllFalse() {
		tv_home.setSelected(false);
		tv_setting.setSelected(false);
		tv_theme.setSelected(false);
		tv_scans.setSelected(false);
		tv_feedback.setSelected(false);
		tv_updates.setSelected(false);
		tv_about.setSelected(false);
		tv_exit.setSelected(false);
	}

	private void hideAllFragment() {
		if (homeFrag != null) {
			transaction.hide(homeFrag);
		}
		if (setFrag != null) {
			transaction.hide(setFrag);
		}
		if (themeFrag != null) {
			transaction.hide(themeFrag);
		}
		if (scanFrag != null) {
			transaction.hide(scanFrag);
		}
		if (feedBackFrag != null) {
			transaction.hide(feedBackFrag);
		}
		if (updateFrag != null) {
			transaction.hide(updateFrag);
		}
		if (aboutFrag != null) {
			transaction.hide(aboutFrag);
		}
		if (exitFragment != null) {
			transaction.hide(exitFragment);
		}
	}

	private void findMenuViews() {
		frame_home = (FrameLayout) findViewById(R.id.frame_home);
		home_layout = (RelativeLayout) findViewById(R.id.home_layout);
		setting_layout = (RelativeLayout) findViewById(R.id.setting_layout);
		theme_layout = (RelativeLayout) findViewById(R.id.theme_layout);
		scans_layout = (RelativeLayout) findViewById(R.id.scans_layout);
		feedback_layout = (RelativeLayout) findViewById(R.id.feedback_layout);
		updates_layout = (RelativeLayout) findViewById(R.id.updates_layout);
		about_layout = (RelativeLayout) findViewById(R.id.about_layout);
		exit_layout = (RelativeLayout) findViewById(R.id.exit_layout);

		tv_home = (TextView) findViewById(R.id.tv_home);
		tv_setting = (TextView) findViewById(R.id.tv_setting);
		tv_theme = (TextView) findViewById(R.id.tv_theme);
		tv_scans = (TextView) findViewById(R.id.tv_scans);
		tv_feedback = (TextView) findViewById(R.id.tv_feedback);
		tv_updates = (TextView) findViewById(R.id.tv_updates);
		tv_about = (TextView) findViewById(R.id.tv_about);
		tv_exit = (TextView) findViewById(R.id.tv_exit);
	}

	private void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setIcon(R.drawable.ic_launcher);//设置home图标
		actionBar.setDisplayShowHomeEnabled(true);//显示home图标
		actionBar.setHomeButtonEnabled(true);//home图标可点击
		actionBar.setDisplayHomeAsUpEnabled(true);//显示导航图标
	}

	private void initDrawerLayout() {
		drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
		//创建监听对(ActionBarDrawerToggle 实现了DrawerLayout.DrawerListener)，添加导航图标
		toggle = new ActionBarDrawerToggle(this, drawerLayout,R.drawable.ic_drawer_am, 0, 0);
		toggle.syncState();// 少了这一行，ic_drawer_am就是默认的图标，不是我们指定的。
		drawerLayout.setDrawerListener(toggle);
	}

	/**
	 * 要想侧栏栏出现，还要加这一行代码：toggle.onOptionsItemSelected(item);
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		toggle.onOptionsItemSelected(item);
		return super.onOptionsItemSelected(item);
	}
}
